const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    imgUrl: app.globalData.imgUrl + 'certificationImage/',
    certifications: [],
    ents: []
  },
  onLoad() {
    let that = this;
    // wx.getStorage({
    //   key: 'userId',
    //   success(res) {
    //     let userId = res.data;
    //     api.queryCertification({
    //       data: {
    //         certificationId: '',
    //         objId: '',
    //         category: '',
    //         account: userId
    //       },
    //       success(res) {
    //         console.log(res.data);
    //         let ct = res.data.Certification;
    //         ct.map((i) => {
    //           i.img = that.data.imgUrl + i.image;
    //         });
    //         that.setData({
    //           ct
    //         });
    //       }
    //     });
    //   }
    // });
    wx.getStorage({
      key: 'role',
      success(res) {
        that.setData({
          role: res.data
        })
        const role = res.data
        if (role === 0) {
          that.getEntAdmin()
        } else {
          that.getEnt()
        }
      }
    })
  },
  getEntAdmin() {
    let that = this
    api.getAllEnterprise({
      data: {
        page: -1,
        limit: -1
      },
      success(response) {
        that.formatEntData(response.data)
      }
    })
  },
  getEnt() {
    let that = this
    if (that.data.role === 4) {
      wx.getStorage({
        key: 'userId',
        success(res) {
          api.queryEnterprise({
            data: {
              account: res.data,
              page: -1,
              limit: -1
            },
            success(response) {
              that.formatEntData(response.data)
            }
          })
        }
      })
    } else {
      wx.getStorage({
        key: 'userId',
        success(res) {
          api.queryPlantation({
            data: {
              account: res.data
            },
            success(response) {
              that.formatEntData(response.data)
            }
          })
        }
      })
    }
  },
  formatEntData(data) {
    let that = this
    if (data.enterprise.length !== 0) {
      let certifications = []
      that.setData({
        ents: data.enterprise
      })
      that.getCertification(0, certifications)
    }
  },
  getCertification(count, certifications) {
    let that = this
    if (count === that.data.ents.length) {
      this.setData({
        certifications
      })
      return
    }
    api.queryCertification_entId({
      data: {
        entId: that.data.ents[count].entId
      },
      success(res) {
        res.data.Certification.map((i) => {
          i.img = that.data.imgUrl + i.image;
        })
        certifications.push(...res.data.Certification)
        that.getCertification(++count, certifications)
      }
    })
  },
  goToAddCertificationPage() {
    wx.navigateTo({
      url: '/pages/addcertification/addcertification',
    })
  },
  goToDetailPage(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/info-display/info-display?id=${id}&type=${type}`,
    })
  }
})